
import { _decorator, AudioClip, AudioSource, Component } from 'cc';
import { Databus } from '../core/Databus';
const { ccclass, property } = _decorator;
const databus = Databus.Instance();

@ccclass('SoundManager')
export class SoundManager extends Component {
  private static instance: SoundManager = null as unknown as SoundManager;
  // 球碰撞声音增加一个冷却时间
  private collisionCoolingTime = 0;
  public static Instance() {
    return SoundManager.instance;
  }
  onLoad(): void {
    SoundManager.instance = this;
    return;
  }

  public playHitBall(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('QiuGanHitBall').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playBallsCollide(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    if (Date.now() < this.collisionCoolingTime + 500) {
      return;
    }
    this.collisionCoolingTime = Date.now();
    const sound = this.node.getChildByName('BallsCollide').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playApplaud(volume: number = 0.25) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('Applaud').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playBallInHoled(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('BallInHoled').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playBiaoChiMove(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('BiaoChiMoved').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public stopBiaoChiMove(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('BiaoChiMoved').getComponent(AudioSource);
    sound.volume = volume;
    sound.stop();
  }

  public playExcellent(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('Excellent').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playGameWin(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('Win').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playGameLose(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('Lose').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playAddScore(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('AddScore').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playForbidHit(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('ForbidHit').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public gainGoldCoin(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('GainGoldCoin').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }

  public playCountDown(volume: number = 0.5) {
    if (databus.gameSetting.soundOn === 0) return;
    const sound = this.node.getChildByName('CountDown').getComponent(AudioSource);
    sound.volume = volume;
    sound.play();
  }
}

export class NetSoundManager {
  private static instance: NetSoundManager = null as unknown as NetSoundManager;
  private bgAudioSource: AudioSource = new AudioSource();
  private constructor() {
    NetSoundManager.instance = this;
  }
  public static Instance() {
    if (!NetSoundManager.instance) {
      new NetSoundManager();
    }
    return NetSoundManager.instance;
  }

  public setBgClip(audioClip: AudioClip) {
    this.bgAudioSource.clip = audioClip;
  }
  public playBgMusic(volume: number = 0.2) {
    this.bgAudioSource.volume = volume;
    this.bgAudioSource.loop = true;
    this.bgAudioSource.play();
  }

  public stopBgMusic() {
    this.bgAudioSource.stop();
  }
}
